<?php
defined('BASEPATH') OR exit('No direct script access allowed');


class Wechat extends CI_Controller
{
    public $host = "http://class.sise.com.cn:7001/sise/";
    public $main_url = "http://class.sise.com.cn:7001/sise/module/student_states/student_select_class/main.jsp";
    public $post_url = 'http://class.sise.com.cn:7001/sise/login_check.jsp';

    //考勤
    public function kaoqin($sid,$password){
        $content = "";
        $status = GetCookies($sid,$password);
        if($status != "error"){
            $main_info = verify_url($this->main_url,$this->post_url,$status,null,0);
            $main_info = mb_convert_encoding($main_info, "UTF-8", "GBK");

            preg_match("/SISEWeb\/pub\/studentstatus\/attendance\/studentAttendanceViewAction\.do\?method=doMain&studentID=(.*)&gzcode=(.*)\'/i", $main_info,$Attendance);
            //var_dump($Attendance);
            $Attendance_url =  "http://class.sise.com.cn:7001/SISEWeb/pub/studentstatus/attendance/studentAttendanceViewAction.do?method=doMain&studentID=$Attendance[1]&gzcode=$Attendance[2]";

            $Attendance_info = verify_url($Attendance_url,$this->main_url,$status,null,0);
            $Attendance_info = mb_convert_encoding($Attendance_info, "UTF-8", "GBK");
            //var_dump($Attendance_info);
            preg_match_all('/<tr class="(odd|even)">([\s\S]*?)<\/tr>/i', $Attendance_info,$Attendance_match);
            // 新建一个数组
            $Attendance_arr = array();
            foreach ($Attendance_match[0] as $value) {
                preg_match('/<td[^>]*>(.*)<\/td>\s<td[^>]*>(.*)<\/td>\s<td[^>]*>(.*)<\/td>/i', $value,$Attendance_data);
                $Attendance_data = array_splice($Attendance_data,1); //去除数组的第一个元素
                $Attendance_arr[] = $Attendance_data; 
            }
            // foreach ($Attendance_arr as $arr_value) {
            //     $content.=$arr_value['1']."-----".$arr_value['2']."<br>";
            // }
            $json_info = json_encode($Attendance_arr,JSON_UNESCAPED_UNICODE);

        }else{
            $content = "服务器出错,请稍后尝试";
        }

        // echo "<pre>";
        // print_r($Attendance_arr);
        // echo "</pre>";
        echo $json_info;
         //var_dump(json_decode($json_info,TRUE));
    }

    //成绩
    public function score($sid,$password){
        $content = "";
        $stuAndscore = array();
        $status = GetCookies($sid,$password);
        if($status != "error"){
            $course_info = verify_url($this->main_url,$this->post_url,$status,null,0);
            $course_info = mb_convert_encoding($course_info, "UTF-8", "GBK");
            preg_match('/courseViewAction\.do\?method=doMain&studentid=(.*)\'/i', $course_info,$match);

            $student_url = "http://class.sise.com.cn:7001/SISEWeb/pub/course/courseViewAction.do?method=doMain&studentid=$match[1]";
            $student_info = verify_url($student_url,$this->main_url,$status,null,0);
            $student_info = mb_convert_encoding($student_info, "UTF-8", "GBK");
            $StudentInfoArr = array();
            preg_match_all('/<td height="16" class="td_left">([\s\S]*?)<\/td>/i', $student_info, $StuInfo);
            $StudentInfoArr = array();
            foreach ($StuInfo[0] as $Stukey => $Stuvalue) {
                preg_match('/<div align="left">([^<]*)<\/div>/i', $Stuvalue,$StuMatch);
                if(isset($StuMatch[1]))
                    $StudentInfoArr[] = trim($StuMatch[1]);
            }
            $class = preg_replace('/\s/i', "", $StuInfo[1][8]);
            $StudentInfoArr[] = $class;
           
            preg_match_all('/<tr class="(odd|even)">([\s\S]*?)<\/tr>/i', $student_info,$table);
            //var_dump($table[0]);
            $array = array();
            $score_array = array();
            foreach ($table[0] as $key => $value) {
                preg_match('/[<td>(.*)<\/td>\s]*<td>(.*)<\/td>\s<td width="120"><a[^>]*>(.*)<\/a><\/td>\s<td>(.*)<\/td>\s<td>(.*)<\/td>\s<td width="150">(.*)<\/td>\s<td width="150">(.*)<\/td>\s<td>(.*)<\/td>\s<td>(.*)<\/td>\s<td>(.*)<\/td>/i', $value,$match_info);
                //var_dump($match_info);
                $match_info = array_splice($match_info,1); //去除数组的第一个元素
                $array[] = $match_info;    
            }
            $stuAndscore['info'] = $StudentInfoArr;
            $stuAndscore['score'] = $array;
            $json_info = json_encode($stuAndscore,JSON_UNESCAPED_UNICODE);
        }else{
            $error_arr = array("服务器出错,请稍后尝试");
            $json_info = json_encode($error_arr,JSON_UNESCAPED_UNICODE);
        }

        echo $json_info;

    }

    //考试时间
    public function exam_time($uid,$password){
        $status = GetCookies($uid,$password);
        if($status != "error"){
            $exam_info = verify_url($this->main_url,$this->post_url,$status,null,0);
            $exam_info = mb_convert_encoding($exam_info, "UTF-8", "GBK");
            preg_match('/SISEWeb\/pub\/exam\/studentexamAction\.do\?method=doMain&studentid=(.+)\'/i', $exam_info,$exam_match);
            $exam_url = "http://class.sise.com.cn:7001/SISEWeb/pub/exam/studentexamAction.do?method=doMain&studentid=$exam_match[1]";
            $ExamData = verify_url($exam_url,$this->main_url,$status,null,0);
            $ExamData = mb_convert_encoding($ExamData, "UTF-8", "GBK");
            // preg_match_all('/<td height="16" class="td_left">[\s\S]*?<\/td>/i', $student_info, $StuInfo);
            preg_match_all('/<tr class="(odd|even)">([\s\S]*?)<\/tr>/i', $ExamData,$table);
            //var_dump($table[0]);

            $array = array();
            foreach ($table[0] as $key => $value) {
                preg_match('/<td[^>]*>(.*)<\/td>\s<td[^>]*>(.+)<\/td>\s<td[^>]*>(.+)<\/td>\s<td[^>]*>(.+)<\/td>\s<td[^>]*>(.+)<\/td>\s<td[^>]*>(.*)<\/td>\s<td[^>]*>(.*)<\/td>\s<td[^>]*>(.*)<\/td>/i', $value,$match_info);
                //var_dump($match_info);
                $match_info = array_splice($match_info,1); //去除数组的第一个元素
                $array[] = $match_info;    
            }
            $json_info = json_encode($array,JSON_UNESCAPED_UNICODE);
        }else{
            $error_arr = array("服务器出错,请稍后尝试");
            $json_info = json_encode($error_arr,JSON_UNESCAPED_UNICODE);
        }
        echo $json_info;
    }

    //奖励惩罚
    public function reward($uid,$password){
        $all_array = array();
        $status = GetCookies($uid,$password);
        if($status != "error"){
            $reward_info = verify_url($this->main_url,$this->post_url,$status,null,0);
            $reward_info = mb_convert_encoding($reward_info, "UTF-8", "GBK");
            preg_match('/sise\/module\/encourage_punish\/encourage_punish\.jsp\?stuname=(.*)&gzcode=(.*)&serialabc=(.*)\'/i', $reward_info,$reward_match);
            $reward_url = "http://class.sise.com.cn:7001/sise/module/encourage_punish/encourage_punish.jsp?stuname=$reward_match[1]&gzcode=$reward_match[2]&serialabc=$reward_match[3]";
            $RewardData = verify_url($reward_url,$this->main_url,$status,null,0);
            $RewardData = mb_convert_encoding($RewardData, "UTF-8", "GBK");
            // preg_match_all('/<td height="16" class="td_left">[\s\S]*?<\/td>/i', $student_info, $StuInfo);
            preg_match_all('/<table width="90%" border="0"  class="table1"  cellspacing="1" align="center">([\s\S]*?)<\/table>/i', $RewardData,$table);
            //再对奖励惩罚两个分开
            preg_match_all('/<tr>[\s\S]*?<\/tr>/i', $table[1][1],$reward_table);
            $array = array();
            foreach ($reward_table[0] as $key => $value) {
                if($key>=2){
                    preg_match('/<td class="tablebody" >(.*)<\/td>\\s+<td class="tablebody" >(.*)<\/td>\\s+<td class="tablebody" >(.*)<\/td>\\s+<td class="tablebody" >(.*)<\/td>\\s+<td class="tablebody" >(.*)<\/td>\\s+<td class="tablebody" >(.*)<\/td>/i', $value,$match);
                    $match = array_splice($match,1);
                    $array[] = $match; 
                }
            }
            $all_array[] = $array;
           

            //惩罚
            preg_match_all('/<tr>[\s\S]*?<\/tr>/i', $table[1][2],$punish_table);
            $punish_arr = array();
            foreach ($reward_table[0] as $punish_key => $punish_value) {
                if($punish_key>=2){
                    preg_match('/<td class="tablebody" >(.*)<\/td>\\s+<td class="tablebody" >(.*)<\/td>\\s+<td class="tablebody" >(.*)<\/td>\\s+<td class="tablebody" >(.*)<\/td>\\s+<td class="tablebody" >(.*)<\/td>\\s+<td class="tablebody" >(.*)<\/td>\\s+<td class="tablebody" >(.*)<\/td>\\s+<td class="tablebody" >(.*)<\/td>/i', $punish_value,$punish_match);
                    $punish_match = array_splice($punish_match,1);
                    //var_dump($punish_match);

                    $punish_arr[] = $punish_match; 
                }
            }
            $all_array[] = $punish_arr;
            $json_info = json_encode($all_array,JSON_UNESCAPED_UNICODE);
        }else{
            $all_array['msg'] = 'wrong' ;
            $json_info = json_encode($all_array,JSON_UNESCAPED_UNICODE);
        }
        echo $json_info;
    }
    
   public function testUSer(){

	$userid = $this->input->post('sid');
        $password = $this->input->post('password');
        $data = GetCookies($userid,$password);
        if($data == "error"){
            echo $data;
            return $data;
        }else{
            echo "ok";
        }
    }    
   
   //课程表
       public function course($uid,$password){
        $status = GetCookies($uid,$password);
        if($status != "error"){
            $course_url = "http://class.sise.com.cn:7001/sise/module/student_schedular/student_schedular.jsp";
            $post_url = "http://class.sise.com.cn:7001/sise/login_check.jsp";
            $course_info = verify_url($course_url,$post_url,$status,null,0);
            $course_info = mb_convert_encoding($course_info, "UTF-8", "GBK");
            preg_match_all('/<span class="style16">([\s\S]*?)<\/span>/i', $course_info, $student_info);
            preg_match('/<font color="#006766" face="宋体">(.*)<\/font>/i',$course_info,$day);
            preg_match_all("/<td width='10%' align='left' valign='top' class='font12'>(.*?)<\/td>/i", $course_info,$course_matches);
            $week = explode(":",trim($student_info[1][1]));
            $var = explode(" ",$day[1]);
            $course_arr = array(
                    'week' => $week[1],
                    'day'  => $var[1],
                    'course' => $course_matches[1],
                    'msg' => 'success'
                );
            $json_info = json_encode($course_arr,JSON_UNESCAPED_UNICODE);
        }else{
            $error['msg'] = "wrong";
            $json_info = json_encode($error,JSON_UNESCAPED_UNICODE); 
        }
        echo $json_info;   
    }


        //晚归,停电
    public function illegal($uid,$password){
        $status = GetCookies($uid,$password);
        $all_array = array();
        if($status != "error"){
            $all_array["msg"] = "success";
            $person_info = verify_url($this->main_url,$this->post_url,$status,null,0);
            $person_info = mb_convert_encoding($person_info, "UTF-8", "GBK");
            preg_match('/SISEWeb\/pub\/studentstatus\/lateStudentAction.do\?method=doMain&gzCode=(.*)&md5Code=(.*)\'/i', $person_info,$match);
            //var_dump($match);
            $illegal_url = "http://class.sise.com.cn:7001/SISEWeb/pub/studentstatus/lateStudentAction.do?method=doMain&gzCode=$match[1]&md5Code=$match[2]";
            $illegal_info = verify_url($illegal_url,$this->post_url,$status,null,0);
            $illegal_info = mb_convert_encoding($illegal_info, "UTF-8","GBK");
            preg_match_all('/<tr class="(odd|even)">[\s\S]*?<\/tr>/i', $illegal_info,$illegal_match);

            foreach ($illegal_match[0] as $value) {
                preg_match_all('/<td>(.*)<\/td>/i', $value,$Match_result);
                $all_array['illegal'][] = $Match_result[1];
            }

            //$json_info = json_encode($all_array,JSON_UNESCAPED_UNICODE);
        }else{
            $all_array["msg"] = "wrong";
            
        }
        $json_info = json_encode($all_array,JSON_UNESCAPED_UNICODE);
        echo $json_info;
        // echo "<pre>";
        // print_r(json_decode($json_info,TRUE));
        // echo "</pre>";
    }
    
}
